package com.bestcem.xm.qdes.controller;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bestcem.xm.common.core.annotation.XmGet;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.component.mybatis.base.ResultPage;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.qdes.controller.param.ImageLibPageParam;
import com.bestcem.xm.qdes.controller.param.ImageLibParam;
import com.bestcem.xm.qdes.controller.param.ImageUploadParam;
import com.bestcem.xm.qdes.controller.vo.DeletedCountVo;
import com.bestcem.xm.qdes.controller.vo.ImagelibVo;
import com.bestcem.xm.qdes.entity.QdesImageLib;
import com.bestcem.xm.qdes.service.QdesImageLibService;
import com.bestcem.xm.qdes.service.UploadFileInfoService;
import com.bestcem.xm.qdes.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@Api(tags = "模板库-图片库")
@RestController
@RequiredArgsConstructor
@RequestMapping("/qdes/qlibs")
public class ImagelibController {

    private @NonNull UploadFileInfoService uploadFileInfoService;

    private @NonNull QdesImageLibService qdesImageLibService;

    /**
     * 图片上传功能
     *
     * @param file 附件
     * @return
     * @author gy.zhang
     * @date 2022/8/10
     */
    @ApiOperation(value = "图片上传功能接口")
    @PostMapping("/imagelib")
    public XmResultJson<List<String>> uploadImagelib(@RequestParam("file") MultipartFile file,
                                                     @RequestParam(name = "systemCode") String systemCode,
                                                     @RequestParam(name = "keep_ext", required = false) Boolean keepext) throws Exception {
        if (Validator.isNull(file)) {
            return XmResultJson.fail(4, ":attachment is required");
        }
        String orgId = SecurityContextHolder.getOrgId();
        String userId = SecurityContextHolder.getUserId();

        ImageUploadParam imageUploadParam = new ImageUploadParam();
        imageUploadParam.setOrgId(orgId);
        imageUploadParam.setUserId(userId);
        imageUploadParam.setSystemCode(systemCode);
        if (ObjectUtil.isNull(keepext)) {
            keepext = false;
        }
        imageUploadParam.setKeepext(keepext);

        //上传图片
        String imageId = uploadFileInfoService.uploadFileInfo(file, imageUploadParam);
        if (ObjectUtil.isNull(imageId)) {
            return XmResultJson.fail(-1, "图片上传失败");
        }
        return XmResultJson.success(ListUtil.of(imageId));
    }

    /**
     * 图片分页查询
     *
     * @return
     * @author gy.zhang
     * @date 2022/8/15
     */
    @ApiOperation(value = "图片分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "page", value = "页码"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "rowsPerPage", value = "每页条数")
    })
    @GetMapping({"/imagelib", "/imagelib/official"})
    public XmResultJson<ResultPage<ImagelibVo>> getImagelib(@ApiIgnore Page<ImagelibVo> page, @XmGet ImageLibPageParam param) {
        page = qdesImageLibService.pageImagelib(page, param);
        //转换返回结构
        ResultPage<ImagelibVo> data = PageUtil.getResultPage(page.getCurrent(), page.getTotal(), page.getRecords());
        return XmResultJson.success(data);
    }

    @ApiOperation(value = "图片库修改图片名称接口")
    @PutMapping("/imagelib/{id:[a-fA-F0-9]{24}}")
    public XmResultJson<List<String>> updateImagelibName(@PathVariable String id,
                                                         @Validated @RequestBody ImageLibParam imageLibParam) {
        QdesImageLib qdesImagelib = new QdesImageLib();
        qdesImagelib.setId(id);
        qdesImagelib.setTitle(imageLibParam.getTitle());
        //创建修改条件对象
        LambdaUpdateWrapper<QdesImageLib> updateWrapper = new LambdaUpdateWrapper<>();
        //构造查询条件
        updateWrapper
                .set(QdesImageLib::getTitle, qdesImagelib.getTitle())
                .eq(QdesImageLib::getId, id);
        boolean b = qdesImageLibService.update(updateWrapper);
        if (b) {
            return XmResultJson.success(ListUtil.of(id));
        } else {
            return XmResultJson.fail(-1, "修改失败");
        }
    }

    @ApiOperation(value = "图片库删除接口")
    @DeleteMapping("/imagelib/{id:[a-fA-F0-9]{24}}")
    public XmResultJson<DeletedCountVo> deleteImagelib(@PathVariable String id) {
        return XmResultJson.success(qdesImageLibService.deleteImagelib(id));
    }
}
